<?php
namespace Home\Model;
use Think\Model;

/**
 * Created by hanquan.
 * User Collaborator
 * Date 2017/04/07
 * Time 15:20
 * MemberModel 成员(城市合伙人、联盟商户、自有工程师、滴滴工程师、联盟商户工程师、联盟商户门店)模型类
 */
class MemberModel extends Model{
	
	private $Member = null;
	function __construct(){
		$this->Member = M('Member');
	}

	/**
	 * 查询成员列表
	 * @param int $member_id 成员id
	 * @param int $type 成员类型，0-全部，1-城市合伙人，2-联盟商户，3-自有工程师，4-滴滴工程师，5-联盟商户工程师，6-联盟商户门店
	 * @return array 二维数组
	 */
	public function get_member_list($type=0,$city_id=FALSE){
		$condition = array();
		if($type){
			$condition['m_type'] = $type;
		}
		if($city_id){
			$condition['mp_city.city_id'] = $city_id;
		}
		$condition['m_status'] = 1;
		return $this->Member
		->join('mp_region ON mp_member.re_id=mp_region.re_id','LEFT')
		->join('mp_city ON mp_member.city_id=mp_city.city_id','LEFT')
		->field('member_id,m_name,m_address,m_business_hours,mp_member.re_id,m_tel')
		->where($condition)->select();
	}

	/**
	 * 查询成员详情
	 * @param int $member_id 成员id
	 * @return array 一维数组
	 */
	public function get_member($member_id){
		$condition['member_id'] = $member_id;
		return $this->Member->where($condition)->find();
	}

	/**
	 * 插入和修改成员
	 * @param array $data 成员数据
	 * @param  int $member_id 成员id
	 */
	public function set_member($data,$member_id=FALSE){

		if(!$member_id){
			return $this->Member->add($data);
		}else{
			$condition['member_id'] = $member_id;
			return $this->Member->where($condition)->save($data);
		}
	}

	/**
	 * 判断是否可以发送短信
	 * @param $phone 手机号
	 */
	public function determine_whether_can_be_sent($phone){
		$resultData = $this->Member->where(array('m_account'=>$phone))->find();

		if(!$resultData['member_id']){
			returnMobileJson(-2,'此手机号没有被注册');
		}

		if($resultData['openid']){
			returnMobileJson(-2,'此账号已绑定');
		}else{


			return true;
		}
	}


	/**
	 * 绑定师傅微信公众号
	 * @param $phone
	 * @return bool
	 */
	public function binding_wechat_id($phone){
		$resultData = $this->Member->where(array('m_account'=>$phone))->find();

		if(!$resultData['member_id']){
			return false;
		}else{
			if($_SESSION['openid']){
				$this->Member->where(array('member_id'=>$resultData['member_id']))->save(array('openid'=>$_SESSION['openid']));
				return true;
			}else{
				return false;
			}
		}
	}


	/**
	 * 获取微信客户端微信
	 */
	public function access_to_master_information($member_id){
		$condition['member_id'] = $member_id;
		return $data = $this->Member->field('member_id,m_name,m_job_num,m_head_imgurl,m_type,m_address,m_business_hours')->where($condition)->find();
	}




	/**
	 * 查询地区所有成员
	 * @param  int $re_id 地区id
	 * @return array 二维数组
	 */
	public function get_region_member($re_id){
		$condition['mp_member.m_type'] = array('between',array(3,5));
		$condition['mp_city.city_id'] = $this->Member->table('mp_region')->where(array('re_id'=>$re_id))->find()['city_id'];
		$condition['m_status'] = 1;
		return $this->Member
				->join('mp_region ON mp_member.re_id = mp_region.re_id','LEFT')
				->join('mp_city ON mp_region.city_id = mp_city.city_id','LEFT')
				->field('member_id,m_name,m_account,city,region')->where($condition)->select();
	}




	/**
	 * 判断账号是否存在
	 * @param  int $account 账号
	 * @return Boolean
	 */
	public function get_exist_member($account){
		$condition['m_account'] = $account;
		if($this->Member->where($condition)->count()){
			return true;
		}else{
			return false;
		}
	}



	/**
	 * 通过账号获取用户信息
	 * @param $m_account
	 * @return mixed
	 */
	public function get_member_by_m_account($m_account){
		return $this->Member->where(array('m_account'=>$m_account))->find();
	}

}